<?php
namespace api\modules\v1\controllers;

use api\controllers\OnAuthController;
use api\modules\v1\models\ModuleSuperior;
use common\models\api\AccessToken;
use common\models\member\MemberAuth;
use common\models\member\MemberInfo;
use Yii;
use common\controllers\BaseController;
use common\components\WechatLoginTrait;

class WechatController extends OnAuthController
{
    use WechatLoginTrait;
    public $modelClass = '';

    protected $optional = ['login'];

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        if (!Yii::$app->wechat->isWechat)
        {
//             die('请用微信打开');
        }

//        Yii::$app->params['simulateUser']['switch'] = true;
        $this->login_default();
    }
    public function index(){

    }

    /**
     * 微信授权登录
     * @return string
     * @throws \yii\base\Exception
     */
    public function actionLogin(){
        $request = Yii::$app->request;
        $url = $request->get('url','');
        $code = $request->get('cc_code','');

        $wechat_member = Yii::$app->params['wechatMember'];
        $member_auth = Yii::$app->params['wechatAuth'];
//        Yii::getLogger()->log(json_encode($wechat_member),'1');
        if($member_auth){
            $member_id = $member_auth['member_id'];
            if(!$member_id){
                //创建memberinfo对象
                $user = new MemberInfo();
                $user->merchant_id = $member_auth['merchant_id'];
                $user->username = Yii::$app->security->generateRandomString(12);
                $user->email = Yii::$app->wechat->user->email;
                $user->head_portrait = $wechat_member['avatar'];
                $user->nickname = $wechat_member['nickname'];
                $user->setPassword('6F3sg%sa');
                $user->generateAuthKey();
                if($user->save()){
                    $member_id = $user->attributes['id'];
                    if($member_id){
                        $MemberAuth = MemberAuth::findOne($member_auth['id']);
                        $MemberAuth->member_id = $member_id;
                        if($MemberAuth->save()){
                        }else
                            return $this->render('error',['name'=>'','message'=>'MemberAuth保存失败']);
                    }else
                        return $this->render('error',['name'=>'','message'=>'memberId不存在']);
                }else
                    return $this->render('error',['name'=>'','message'=>'MemberInfo保存失败']);
            }
            $member = MemberInfo::findOne($member_id);
            $token = AccessToken::getAccessToken($member,'app');

            if($token['access_token']){
                if(!empty($code)){
                    ModuleSuperior::createSuperior($member_id,$code);
                }
//                return $token;
                if($url){
                    header("location:" . $url.'?token='.$token['access_token']);
                    exit;
                }else{
                    return $this->render('login', [
                        'wechat_member'=> $wechat_member,
                        'url'=> $url.'?token='.$token['access_token'],
                        'token' => $token
                    ]);
                }
            }
        }else
            return $this->render('error',['name'=>'','message'=>'']);
    }
}